package com.maomao.check;

//import java.sql.*;
import java.io.IOException;

import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.maomao.db.DbConnection;
import com.maomao.beans.*;
import com.maomao.handler.*;
public class checkUser extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public checkUser() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		
		String userName = request.getParameter("userName");
		String password = request.getParameter("userPassword");
		String userType = request.getParameter("userType");
	
		User user = factory.create(request);
		
		checkProcess cp = new checkProcess();
		
		cp.checkParameters(user, request, response);
		
		
//		String sql = "select * from user where username = '"+userName+"' and password = '" +password+"'";
		
//		DbConnection dbc = new DbConnection();
//		
//		ResultSet r = null;
//
//		boolean flag = true;
//		
//		if (userType.equals("student")) {
//			String sql = "select * from student where name = '"+userName+"' and password = '" +password+"'";
//			try {
//				r = dbc.excuteQuery(sql);
//			} catch (ClassNotFoundException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			} catch (SQLException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//			try {
//				if(r.next()){
//					student stu = new student();
//					stu.setName(r.getString("name"));
//					stu.setStudentId(r.getString("id"));
//					stu.setPasswd(r.getString("password"));
//					flag = false;
//				
//				}
//			} catch (SQLException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}
//		else if (userType.equals("teacher")) {
//			String sql = "select * from teacher where name = '"+userName+"' and password = '" +password+"'";
//			try {
//				r = dbc.excuteQuery(sql);
//			} catch (ClassNotFoundException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			} catch (SQLException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//			try {
//				if(r.next()){
//					teacher t = new teacher();
//					t.setName(r.getString("name"));
//					t.setTeacherId(r.getString("id"));
//					t.setPasswd(r.getString("password"));
//					flag = false;
//					
//				}
//			} catch (SQLException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			} 
//		}
//		else if (userType.equals("ta")) {
//			String sql = "select * from ta where name = '"+userName+"' and password = '" +password+"'";
//			try {
//				r = dbc.excuteQuery(sql);
//			} catch (ClassNotFoundException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			} catch (SQLException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//			try {
//				if(r.next()){
//					ta t = new ta();
//					t.setName(r.getString("name"));
//					t.setTaId(r.getString("id"));
//					t.setPasswd(r.getString("password"));
//					flag = false;
//					
//				}
//			} catch (SQLException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		
//		} 
//		
//		boolean flag = false;
//		Chain chain = new Chain();
//		flag = chain.doIt(request);
//		
//		
//		if(!flag)
//			out.println("<script language='javascript'>alert('Your password or your username is wrong!!'); location='login.jsp';</script>");
//		else
//		{
//			
//			session.setAttribute("username", userName);
//			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//			String logTime = dateFormat.format(new Date());
//			
//			session.setAttribute("logTime", logTime);
//		
//			
//			Cookie cookie = new Cookie("username", userName);
//			
//			cookie.setMaxAge(60*2);
//			
//			response.addCookie(cookie);
//			response.sendRedirect("info/"+userType+"/welcome.html");
//			
//		}
//			
	
		
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
		System.out.println("init");
	}

}
